﻿namespace Rzrlf.SP.Innovator.WebParts.CompositeWebPart
{
    public enum DisplayState
    {
        Attributes,
        Documents,
        Reports,
        Tasks,
        Geometry
    }

    public partial class CompositeWebPartUserControl : System.Web.UI.UserControl
    {
        private bool _isDisplayChange = false;
        private bool _isNewId = false;

        #region Properties...

        private Rzrlf.SP.Innovator.WebParts.IStructureData _data = null;

        public Rzrlf.SP.Innovator.WebParts.IStructureData Data
        {
            get
            {
                return _data;
            }

            set
            {
                _data = value;

                this.SetCurrentItemId();
                this.SetCurrentXml();

                if (string.IsNullOrEmpty(this.hdnCurrentXml.Value) && this.hdnCurrentDisplayState.Value != DisplayState.Geometry.ToString())
                {
                    this.lblData.Controls.Clear();
                    this.lblData.Text = "No records found";
                }
                else
                {
                    this.HandleData();
                }
            }
        }

        #endregion

        #region Event Handlers...

        protected void OnCompositeDisplayChange(object sender, System.EventArgs e)
        {
            System.Web.UI.WebControls.LinkButton button = sender as System.Web.UI.WebControls.LinkButton;

            if (button != null && !string.IsNullOrEmpty(this.hdnCurrentItemId.Value))
            {
                this.ResetTabStyles();

                this.PersistDisplayState(button);

                button.CssClass = "tabActive";

                _isDisplayChange = true;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!base.IsPostBack)
            {
                this.hdnCurrentDisplayState.Value = DisplayState.Attributes.ToString();
                this.hdnInnovatorDatabaseName.Value = Rzrlf.SP.Innovator.WebParts.Settings.InnovatorDatabaseName;
                this.hdnInnovatorUserName.Value = Rzrlf.SP.Innovator.WebParts.Settings.InnovatorUserName;
                this.hdnInnovatorPassword.Value = Rzrlf.SP.Innovator.WebParts.Settings.InnovatorPassword;
                this.hdnInnovatorServerUrl.Value = Rzrlf.SP.Innovator.WebParts.Settings.InnovatorServerUrl;

                this.btnAttributes.CssClass = "tabActive";
            }
        }

        #endregion

        #region Helper Methods...

        private void HandleData()
        {
            string urlXsl = string.Empty;

            if (this.hdnCurrentDisplayState.Value == DisplayState.Attributes.ToString())
            {
                urlXsl = base.Page.Request.MapPath("/_layouts/Rzrlf.SP.Innovator.WebParts/Attributes.xslt");
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Documents.ToString())
            {
                urlXsl = base.Page.Request.MapPath("/_layouts/Rzrlf.SP.Innovator.WebParts/Documents.xslt");
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Reports.ToString())
            {
                urlXsl = base.Page.Request.MapPath("/_layouts/Rzrlf.SP.Innovator.WebParts/ChangeOrders.xslt");
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Tasks.ToString())
            {
                urlXsl = base.Page.Request.MapPath("/_layouts/Rzrlf.SP.Innovator.WebParts/Tasks.xslt");
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Geometry.ToString())
            {
                string ascxPath = @"~/_CONTROLTEMPLATES/Rzrlf.SP.Innovator.WebParts/CompositeWebPart/ViewerWebPartUserControl.ascx";

                Rzrlf.SP.Innovator.WebParts.CompositeWebPart.ViewerWebPartUserControl ctrl = base.Page.LoadControl(ascxPath) as Rzrlf.SP.Innovator.WebParts.CompositeWebPart.ViewerWebPartUserControl;
                ctrl.Data = _data;

                this.lblData.Text = string.Empty;
                this.lblData.Controls.Clear();
                this.lblData.Controls.Add(ctrl);
            }

            if (!string.IsNullOrEmpty(urlXsl))
            {
                System.Xml.XPath.XPathDocument xmlDoc = new System.Xml.XPath.XPathDocument(new System.IO.StringReader(this.hdnCurrentXml.Value));

                System.Xml.Xsl.XslCompiledTransform xslDoc = new System.Xml.Xsl.XslCompiledTransform();
                xslDoc.Load(urlXsl);

                System.IO.StringWriter writer = new System.IO.StringWriter();
                xslDoc.Transform(xmlDoc, null, writer);

                string content = writer.ToString();
                content = content.Replace("xmlns:asp=\"remove\"", string.Empty);

                System.Web.UI.Control ctrl = base.Page.ParseControl(content);

                this.lblData.Text = string.Empty;
                this.lblData.Controls.Clear();
                this.lblData.Controls.Add(ctrl);
            }
        }

        private void PersistDisplayState(System.Web.UI.WebControls.LinkButton button)
        {
            if (button.ID == this.btnAttributes.ID)
            {
                this.hdnCurrentDisplayState.Value = DisplayState.Attributes.ToString();
            }
            else if (button.ID == this.btnDocuments.ID)
            {
                this.hdnCurrentDisplayState.Value = DisplayState.Documents.ToString();
            }
            else if (button.ID == this.btnChangeOrders.ID)
            {
                this.hdnCurrentDisplayState.Value = DisplayState.Reports.ToString();
            }
            else if (button.ID == this.btnTasks.ID)
            {
                this.hdnCurrentDisplayState.Value = DisplayState.Tasks.ToString();
            }
            else if (button.ID == this.btnGeometry.ID)
            {
                this.hdnCurrentDisplayState.Value = DisplayState.Geometry.ToString();
            }
        }

        private void ResetTabStyles()
        {
            if (this.hdnCurrentDisplayState.Value == DisplayState.Attributes.ToString())
            {
                this.btnAttributes.CssClass = "tabNonActive";
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Documents.ToString())
            {
                this.btnDocuments.CssClass = "tabNonActive";
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Reports.ToString())
            {
                this.btnChangeOrders.CssClass = "tabNonActive";
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Tasks.ToString())
            {
                this.btnTasks.CssClass = "tabNonActive";
            }
            else if (this.hdnCurrentDisplayState.Value == DisplayState.Geometry.ToString())
            {
                this.btnGeometry.CssClass = "tabNonActive";
            }
        }

        private void SetCurrentItemId()
        {
            if (_data != null && !string.IsNullOrEmpty(_data.CurrentItemXml))
            {
                System.Xml.XPath.XPathDocument xmlDoc = new System.Xml.XPath.XPathDocument(new System.IO.StringReader(_data.CurrentItemXml));

                System.Xml.XPath.XPathNavigator xmlNav = xmlDoc.CreateNavigator();

                System.Xml.XmlNamespaceManager xmlNsMgr = new System.Xml.XmlNamespaceManager(xmlNav.NameTable);
                xmlNsMgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");

                string id = xmlNav.SelectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Body/Result/Item/id", xmlNsMgr).Value;

                string url3d = string.Empty;
                if (xmlNav.SelectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Body/Result/Item/url_3d", xmlNsMgr) != null)
                {
                    url3d = xmlNav.SelectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Body/Result/Item/url_3d", xmlNsMgr).Value;
                }

                if (string.IsNullOrEmpty(url3d))
                {
                    this.btnGeometry.Visible = false;

                    if (this.hdnCurrentDisplayState.Value == DisplayState.Geometry.ToString())
                    {
                        this.hdnCurrentDisplayState.Value = DisplayState.Attributes.ToString();

                        this.btnGeometry.CssClass = "tabNonActive";

                        this.btnAttributes.CssClass = "tabActive";
                    }
                }
                else
                {
                    this.btnGeometry.Visible = true;
                }

                if (id != this.hdnCurrentItemId.Value)
                {
                    _isNewId = true;

                    this.hdnCurrentItemId.Value = id;
                }
            }
        }

        private void SetCurrentXml()
        {
            if (_isDisplayChange || _isNewId)
            {
                if (!string.IsNullOrEmpty(this.hdnCurrentItemId.Value))
                {
                    if (string.IsNullOrEmpty(this.hdnCurrentDisplayState.Value))
                    {
                        this.hdnCurrentDisplayState.Value = DisplayState.Attributes.ToString();
                    }

                    if (this.hdnCurrentDisplayState.Value == DisplayState.Attributes.ToString())
                    {
                        if (_data != null)
                        {
                            this.hdnCurrentXml.Value = _data.CurrentItemXml;
                        }
                        else
                        {
                            this.hdnCurrentXml.Value = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetPart(this.hdnCurrentItemId.Value);
                        }
                    }
                    else if (this.hdnCurrentDisplayState.Value == DisplayState.Documents.ToString())
                    {
                        this.hdnCurrentXml.Value = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetPartDocuments(this.hdnCurrentItemId.Value);
                    }
                    else if (this.hdnCurrentDisplayState.Value == DisplayState.Reports.ToString())
                    {
                        this.hdnCurrentXml.Value = Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.GetPartECOs(this.hdnCurrentItemId.Value);
                    }
                    else if (this.hdnCurrentDisplayState.Value == DisplayState.Tasks.ToString())
                    {
                        Microsoft.SharePoint.SPList list = Microsoft.SharePoint.SPContext.Current.Web.Lists["Change Requests"];

                        string syntax = string.Format("<Where><Eq><FieldRef Name='{0}' /><Value Type='Text'>{1}</Value></Eq></Where>", list.Fields["Part Id"].InternalName, this.hdnCurrentItemId.Value);

                        Microsoft.SharePoint.SPQuery query = new Microsoft.SharePoint.SPQuery();
                        query.Query = syntax;

                        this.hdnCurrentXml.Value = list.GetItems(query).Xml;
                    }
                    else if (this.hdnCurrentDisplayState.Value == DisplayState.Geometry.ToString())
                    {
                        this.hdnCurrentXml.Value = string.Empty;
                    }
                }
                else
                {
                    this.hdnCurrentXml.Value = string.Empty;
                }
            }
        }

        #endregion
    }
}